package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ProjectCountListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ProjectCollectOriginal;
import com.isunland.managesystem.entity.ProjectCollectOriginal.ProjectCollectContent;
import com.isunland.managesystem.entity.ProjectSearchContent;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;

public class ProjectCountListFragment extends BaseListFragment {
  public static final String EXTRA_CONTENT =
      "com.isunland.managesystem.entity.ProjectCountListFragment.EXTRA_CONTENT";
  private int SEARCH_COUNT = 0;
  private ArrayList<ProjectCollectContent> mlist;
  private ProjectCountListAdapter mAdapter;
  private ProjectSearchContent mSearchContent;

  private String customerId = "";
  private String customerName = "";

  //	    private String propertyCode="";//性质
  //	    private String propertyName="";

  private String projectState = "";//阶段
  private String projectStateName = "";//阶段名

  private String startDateBegin = "";//(开工日期 开始)
  private String startDateEnd = "";//(开工日期 结束)

  private String projectName = "";//项目名称

  //		private String requireDateBegin="";//(开工日期 开始)
  //		private String requireDateEnd="";//(开工日期 结束)

  //		private String ifOver="";
  //		private String ifOverName="";

  private String pmId = "";
  private String pmName = "";

  //		private String projectTypeCode="";
  //		private String projectTypeName="";

  private String projectKindName = "";
  private String projectKindCode = "";

  //		private String salesId="";
  //		private String salesName="";

  //		private String queryColsStr="pm_name,PARTA_NAME,DISTRICT_DESC,STAGES,require_date,Finish_DATE,if_acceptance";
        /*新修改统计项*/
  private String queryColsStr =
      "PROJECT_KIND_NAME,STAGES,PARTA_NAME,pm_name,require_date,PROJECT_SIGN_DATE,if_acceptance";
  private ProjectCountContent queryContent;

  @Override public void onCreate(Bundle savedInstanceState) {

    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar()
        .setTitle(R.string.project_count_list);
    mSearchContent = (ProjectSearchContent) getArguments().getSerializable(EXTRA_CONTENT);
    getData(mSearchContent);
    //		queryContent=new ProjectCountContent("pm_name","PARTA_NAME","DISTRICT_DESC","STAGES","require_date","Finish_DATE","if_acceptance");
    queryContent = new ProjectCountContent("PROJECT_KIND_NAME", "STAGES", "PARTA_NAME", "pm_name",
        "require_date", "PROJECT_SIGN_DATE", "if_acceptance");
    super.onCreate(savedInstanceState);
  }

  private void getData(ProjectSearchContent content) {

    customerId = content.getCustomerId();
    customerName = content.getCustomerName();

    //	    propertyCode=content.getPropertyCode();//性质
    //	    propertyName=content.getPropertyName();

    projectState = content.getProjectState();//阶段
    projectStateName = content.getProjectStateName();//阶段名

    startDateBegin = content.getStartDateBegin();//(开工日期 开始)
    startDateEnd = content.getStartDateEnd();//(开工日期 结束)

    projectName = content.getProjectName();//项目名称

    //		requireDateBegin=content.getRequireDateBegin();//(开工日期 开始)
    //		requireDateEnd=content.getRequireDateEnd();//(开工日期 结束)
    //
    //		ifOver=content.getIfOver();
    //		ifOverName=content.getIfOverName();

    pmId = content.getPmId();
    pmName = content.getPmName();

    //		projectTypeCode=content.getProjectTypeCode();
    //		projectTypeName=content.getProjectTypeName();

    projectKindName = content.getProjectKindName();
    projectKindCode = content.getProjectKindCode();

    //		salesId=content.getSalesId();
    //		salesName=content.getSalesName();

  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      case R.id.menu_item_query:

        Intent i = new Intent(getActivity(), SearchProjectCountActivity.class);
        i.putExtra(SearchProjectCountFragment.EXTRA_CONTENT, queryContent);
        startActivityForResult(i, SEARCH_COUNT);
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {

    return ApiConst.ULR_PROJECT_COUNT;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    //		params.put("customerId", customerId);
    //		params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    //		params.put("dataStatus", "publish");
    //		params.put("startDateBegin", startDateBegin);
    //		params.put("startDateEnd", startDateEnd);
    //		params.put("requireDateBegin", requireDateBegin);
    //		params.put("requireDateEnd", requireDateEnd);
    //		params.put("projectState", projectState);
    //		params.put("ifOver", ifOver);
    //		params.put("projectPropertyCode", propertyCode);
    //		params.put("projectTypeCode", projectTypeCode);
    //		params.put("projectKindCode", projectKindCode);
    //		params.put("projectName", projectName);
    //		params.put("pmId", pmId);
    //		params.put("salesId", salesId);
    //		params.put("queryColsStr",queryColsStr);
    //		params.put("page", "1");
    //		params.put("rows", "1000");

    /**
     * 新修改
     */

    params.put("customerId", customerId);
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("dataStatus", "publish");
    params.put("beginprojectSignDate", startDateBegin);
    params.put("endprojectSignDate", startDateEnd);
    params.put("projectState", projectState);
    params.put("projectKindCode", projectKindCode);
    params.put("projectName", projectName);
    params.put("pmId", pmId);
    params.put("queryColsStr", queryColsStr);
    params.put("page", "1");
    params.put("rows", "1000");
    return params;
  }

  @Override public void resolveResponse(String json) {
    ProjectCollectOriginal original = new Gson().fromJson(json, ProjectCollectOriginal.class);
    ArrayList<ProjectCollectContent> rows = (ArrayList<ProjectCollectContent>) original.getRows();
    if (mlist == null) {
      mlist = new ArrayList<ProjectCollectOriginal.ProjectCollectContent>();
    }
    mlist.clear();
    mlist.addAll(rows);
    setAdapter(mlist, queryContent);
  }

  @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) {
    volleyPost();
    super.onRefresh(refreshView);
  }

  private void setAdapter(ArrayList<ProjectCollectContent> mlist2,
      ProjectCountContent queryContent) {
    //		if (mAdapter==null) {
    //			mAdapter=new ProjectCountListAdapter(getActivity(), mlist2);
    ////			mAdapter=new ProjectCountListAdapter(getActivity(), mlist2,queryContent);
    //		}
    mAdapter = new ProjectCountListAdapter(getActivity(), mlist2, queryContent);
    setListAdapter(mAdapter);
    ((ProjectCountListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  //	@Override
  //	public void onListItemClick(ListView l, View v, int position, long id) {
  //		ProjectCollectContent content=mlist.get(position-1);
  //		Intent i=new Intent(getActivity(),DetailProjectCountActivity.class);
  //		i.putExtra(DetailProjectCountFragment.EXTRA_CONTENT, content);
  //		i.putExtra(DetailProjectCountFragment.EXTRA_QUERY, queryContent);
  //		startActivity(i);
  //		super.onListItemClick(l, v, position, id);
  //	}
  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == SEARCH_COUNT) {
      queryContent = (ProjectCountContent) data.getSerializableExtra("queryContent");
      StringBuffer str = new StringBuffer();
      //			if (!TextUtils.isEmpty(queryContent.getPmName())) {
      //				str.append(queryContent.getPmName()).append(",");
      //			}
      //			if (!TextUtils.isEmpty(queryContent.getCustomer())) {
      //				str.append(queryContent.getCustomer()).append(",");
      //			}
      //			if (!TextUtils.isEmpty(queryContent.getDesc())) {
      //				str.append(queryContent.getDesc()).append(",");
      //			}
      //			if (!TextUtils.isEmpty(queryContent.getStages())) {
      //				str.append(queryContent.getStages()).append(",");
      //			}
      //			if (!TextUtils.isEmpty(queryContent.getRequieDate())) {
      //				str.append(queryContent.getRequieDate()).append(",");
      //			}
      //			if (!TextUtils.isEmpty(queryContent.getFinishDate())) {
      //				str.append(queryContent.getFinishDate()).append(",");
      //
      //			}
      //			if (!TextUtils.isEmpty(queryContent.getIfCheck())) {
      //				str.append(queryContent.getIfCheck());
      //			}

      //新修改

      if (!TextUtils.isEmpty(queryContent.getProjectKindName())) {
        str.append(queryContent.getProjectKindName()).append(",");
      }
      if (!TextUtils.isEmpty(queryContent.getStages())) {
        str.append(queryContent.getStages()).append(",");
      }
      if (!TextUtils.isEmpty(queryContent.getPartaName())) {
        str.append(queryContent.getPartaName()).append(",");
      }
      if (!TextUtils.isEmpty(queryContent.getPmName())) {
        str.append(queryContent.getPmName()).append(",");
      }
      if (!TextUtils.isEmpty(queryContent.getRequireDate())) {
        str.append(queryContent.getRequireDate()).append(",");
      }
      if (!TextUtils.isEmpty(queryContent.getProjectSignDate())) {
        str.append(queryContent.getProjectSignDate()).append(",");
      }
      if (!TextUtils.isEmpty(queryContent.getIfAcceptance())) {
        str.append(queryContent.getIfAcceptance());
      }

      queryColsStr = str.toString();
      //最后一位是","
      if (queryColsStr.lastIndexOf(",") == queryColsStr.length() - 1) {
        queryColsStr = queryColsStr.substring(0, queryColsStr.length() - 1);
      }
      LogUtil.i("queryColsStr====" + queryColsStr);
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static ProjectCountListFragment newInstance(ProjectSearchContent item) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, item);
    ProjectCountListFragment fragment = new ProjectCountListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
